﻿using System;
using System.Linq;
using Core.Contract.DataContract;
using www.Models;

namespace Data.DAO
{
    public class StaffDAO //: BaseDAO<RELAXEntities, RelaxManagerMember>
   {
        #region value
        private readonly RelaxDataContext _context = new RelaxDataContext();
        #endregion

       #region hàm lấy dữ liệu

       public IQueryable<RelaxStaffDataContract> GetStaffDataContracts()
       {
           var query = (from p in _context.RelaxManagerMembers
               join l in _context.RelaxLogins on p.id equals l.Staffid
               select new RelaxStaffDataContract
               {
                   Id = Convert.ToInt32(p.id),
                   Image = p.image,
                   Gender = Convert.ToInt32(p.gender),
                   Birthday = Convert.ToDateTime(p.birthday),
                   Identification = Convert.ToInt32(p.identification),
                   Tel = p.tel,
                   Degree = p.degree,
                   Ministry = p.ministry,
                   SalaryRatio = Convert.ToDouble(p.salaryratio),
                   Salary = Convert.ToDouble(p.salary),
                   Allowance = Convert.ToDouble(p.allowance),
                   Hometown = p.hometown,
                   DateBegin = Convert.ToDateTime(p.datebegin),
                   DateEnd = Convert.ToDateTime(p.dateend),
                   Email = p.email,
                   Active= Convert.ToInt32(p.active),
                   Name = p.name,
                   LoginId = Convert.ToInt32(l.id),
                   UserName = l.username
               }).Distinct();
         
           return query;
       }
       public IQueryable<RelaxStaffDataContract> GetStaffDataContract()
       {
           var query = from p in _context.RelaxManagerMembers
                       select new RelaxStaffDataContract
                       {
                           Id = Convert.ToInt32(p.id),
                           Image = p.image,
                           Gender = Convert.ToInt32(p.gender),
                           Birthday = Convert.ToDateTime(p.birthday),
                           Identification = Convert.ToInt32(p.identification),
                           Tel = p.tel,
                           Degree = p.degree,
                           Ministry = p.ministry,
                           SalaryRatio = Convert.ToInt32(p.salaryratio),
                           Salary = Convert.ToInt32(p.salaryratio),
                           Allowance = Convert.ToInt32(p.allowance),
                           Hometown = p.hometown,
                           DateBegin = Convert.ToDateTime(p.datebegin),
                           DateEnd = Convert.ToDateTime(p.dateend),
                           Email = p.email,
                           Active = Convert.ToInt32(p.active),
                           Name = p.name
                       };

           return query;
       }
       #endregion
       #region Add
       public void Add(RelaxManagerMember managerMember)
       {
           _context.RelaxManagerMembers.InsertOnSubmit(managerMember);
           _context.SubmitChanges();

       }
       #endregion
       #region hàm Update

       public void Update(RelaxManagerMember staffRef)
       {
           var obj = _context.RelaxManagerMembers.Where(m => m.id == staffRef.id);
           if (obj.Any())
           {
               var firstOrdefault = obj.FirstOrDefault();
               if (firstOrdefault != null)
               {
                   firstOrdefault.active = staffRef.active;
                   firstOrdefault.image = staffRef.image;
                   firstOrdefault.gender = staffRef.gender;
                   firstOrdefault.birthday = staffRef.birthday;
                   firstOrdefault.identification = staffRef.identification;
                   firstOrdefault.tel = staffRef.tel;
                   firstOrdefault.degree = staffRef.degree;
                   firstOrdefault.ministry = staffRef.ministry;
                   firstOrdefault.salaryratio = staffRef.salaryratio;
                   firstOrdefault.salary = staffRef.salary;
                   firstOrdefault.allowance = staffRef.allowance;
                   firstOrdefault.hometown = staffRef.hometown;
                   firstOrdefault.datebegin = staffRef.datebegin;
                   firstOrdefault.dateend = staffRef.dateend;
                   firstOrdefault.email = staffRef.email;
                   firstOrdefault.name = staffRef.name;
                   _context.SubmitChanges();
               }
           }
       }
       #endregion

       #region hàm Delete

       public void Delete(long iId)
       {
           var obj = _context.RelaxManagerMembers.FirstOrDefault(m => m.id == iId);
           if (obj!=null)
           {
                // ReSharper disable once AssignNullToNotNullAttribute
               _context.RelaxManagerMembers.DeleteOnSubmit(obj);
               _context.SubmitChanges();
           }
       }
       #endregion

   }
}
